/**************************************************************************
 * @doc CPAC
 * @module CPAC.C - Source code for the accumulator handling functions.|  
 * Source code for the accumulator handling functions.
 *
 * Product     :   SoftPay 2000 (Global Payment Methodology).
 * <nl>Developer   :   SoftPay Development Team (I_SP2000).
 * <nl>Notes       :
 *
 * @head3 CPAC Functions |
 * @index | CPAC
 * @end
 *
 * Copyright (c) 1996-2000 by VeriFone Inc. All rights reserved.
 * 
 * No part of this software may be used, stored, compiled, reproduced,
 * modified, transcribed, translated, transmitted, or transferred, in any form 
 * or by any means whether electronic, mechanical, magnetic, optical, 
 * or otherwise, without the express prior written permission of VeriFone, Inc.
 *
 * Revision History
 * ------------------------------------------------------------------------

* ------------------------------------------------------------------------
 ********************************************************************************/
#include <project.h>


#define CPAC_CF

#include <aclconio.h>
#include <stdio.h>
#include <stdlib.h>
#include <acldev.h>
#include <aclstr.h>
#include <aclutil.h>
#include <string.h>

#include <define.h>
#include <transact.h>
#include <protocol.h>
#include <packet.h>
#include <tran.h>
#include <comm.h>
#include <modem.h>
#include <ppt.h>
#include <piputl.h>
#include <pipmsg.h>
#include <isoutl.h>
#include <isoload.h>
#include <ifldtbl.h>
#include <msg.h>
#include <msgfuncs.h>
#include <bdt.h>
#include <hdt.h>
#include <trt.h>
#include <mht.h>
#include <iit.h>
#include <accum.h>
#include <form.h>
#include <date.h>
#include <mem.h>
#include <spdebug.h>
#include <funcids.h>
#include <pbatch.h>
#include <errno.h>
#include <ascii.h>
#include <amexhost.h>
#include <confio.h>
#include <confproto.h>


#include "cpacsize.h"
#include "cpacids.h"
#include "cpacsrc.h"
#include "cpacbat.h"
#include "cpacmsg.h"
#include "cpacoper.h"
#include "cpatran.h"
#include "cpacprnt.h"
#include "cpacstl.h"
#include "cpac.h"
#include "cpacextn.h"


#define SIZE_CPAC_REC        REC_SIZEOF(CPAC_REC)

CPAC_REC srCPAC;


/********************************************************************************************* 
* @func void vdGetCPACAmtAlwd(char *pchTotAmtAllwd)
*
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdGetCPACAmtAlwd(char* pchTotAmtAllwd) {
    strcpy(pchTotAmtAllwd, srCPAC.szAmtAlwd);
}

/********************************************************************************************* 
* @func void vdSetCPACPresentAmt(char *pchPresentAmt)
* Load the Transaction Amount - ISO Field # 4 
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdSetCPACPresentAmt(char* pchPresentAmt) {
    strcpy(srCPAC.szPresentAmt, pchPresentAmt);
}

/********************************************************************************************* 
* @func void vdGetCPACPresentAmt(char *pchPresentAmt)
* Load the Transaction Amount - ISO Field # 4 
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

void vdGetCPACPresentAmt(char* pchPresentAmt) {
    strcpy(pchPresentAmt, srCPAC.szPresentAmt);
}

/********************************************************************************************* 
* @func void vdGetCPACCurDate(char *pchCurDate)
* 
*
* @parm char * | pchCurDate | 
*       This is the current date.
*
* @end
**********************************************************************************************/

void vdGetCPACCurDate(char* pchCurDate) {
    strcpy(pchCurDate, srCPAC.szDate);
}


/********************************************************************************************* 
* @func void vdSetCPACCurDate(char *pchCurDate)
* 
*
* @parm char * | pchCurDate | 
*       This is the current date.
*
* @end
**********************************************************************************************/


void vdSetCPACCurDate(char* pchCurDate) {
    strcpy(srCPAC.szDate, pchCurDate);
}



/********************************************************************************************* 
* @func int inSaveCPACRec (&srCPAC)
* Load the Transaction Amount - ISO Field # 4 
*
* @parm TRANSACTION_OBJECT * | pobTran | 
*       This is the transaction object.
*
* @end
**********************************************************************************************/

int inSaveCPACRec(CPAC_REC* psrCPAC) {
    char szHostName[30];
    int inMerchantNum;

    memset(szHostName, 0, sizeof(szHostName));

    /** /pdebug (("--vdLoadAmexParams--"));*/
    strcpy(szHostName, szGetHostFileName()); //CPAC2.1B


    strcat(szHostName, ".dat");

    inMerchantNum = inGetMerchantNum() - 1;

    if (inSaveConfRec(szHostName, SIZE_CPAC_REC, inMerchantNum, (char *) psrCPAC) != VS_SUCCESS) {
        pdebug(("inSaveCPACRecErr=%d", 0));
        vdSGErrorMessage(MIT_ERR_MSG21); 
        return(VS_ERR);
    }

    return (VS_SUCCESS);
}

/*-----------------------------------------------------------------------------
* Routine Name : int inChkTransAlwd(TRANSACTION_OBJECT *pobTran, VS_BOOL fChkCPACUpdate)
* Description  : This function accepts the amount for the transaction 
*                if transaction amount matches the different criteria of
*                total transaction amount allowed and current transaction 
*                total in the terminal
* Inputs       : None
* Outputs      : None
* Returns      : VB_SUCCESS/VB_ERR
* Notes        : 
*---------------------------------------------------------------------------
*/
int inChkTransAlwd(TRANSACTION_OBJECT* pobTran,VS_BOOL fChkCPACUpdate) {
    int inTranType;
    long lnTotTransAmt = 0L,
    lnOldTransAmt,
    lnNewTransAmt; 
    char /*szAmt1[AMT_SIZE + 1], szAmt2[AMT_SIZE + 1], */ szAmt[AMT_SIZE + 1]; /*, szDispBuf[20]; CPAC_LINT removed */ 
    char szTotAmtAllwd[AMT_SIZE + 1],
    szPresentAmt[AMT_SIZE + 1];

    //   CPAC_REC srCPAC;



    /*   if (fChkCPACUpdate == VS_FALSE) {  
               if (inRunFunction (pobTran, CPAC_CHK_LONG_AMT_OVERFLOW) != VS_SUCCESS)
                return (VS_ERR);
       } */

    vdLoadCPACParams(&srCPAC);  
    vdGetCPACAmtAlwd(szTotAmtAllwd);
    if (atol(szTotAmtAllwd) == 0L)
        return(VS_SUCCESS);    

    vdGetCPACPresentAmt(szPresentAmt);  
    inTranType = pobTran->srTRTRec.TransactionCode;
    switch (inTranType) {
    case SALE:
    case SALE_OFFLINE:
    case REFUND:
    case PRE_AUTH:
        lnTotTransAmt = pobTran->srBRec.lnBaseTransactionAmount;
        if (inGetTipProcessing())
            lnTotTransAmt = pobTran->srBRec.lnBaseTransactionAmount + pobTran->srBRec.lnTipAmount;  
        break;
    case ADJUST:
    case CPAC_PHIL_ADJUST:
        //1.4_citiphil_don_26feb_DSC
    case ADJUST_REFUND_OFFLINE:
    case PRE_COMP:
        lnOldTransAmt = pobTran->srBRec.lnOldTotalTransactionAmount;              /* Org Amount for Adjust Txn */
        lnNewTransAmt = pobTran->srBRec.lnBaseTransactionAmount;    
        lnTotTransAmt = lnNewTransAmt - lnOldTransAmt;
        break;
    case ADD_TIP_OFFLINE:
    case TIP_ADJUST:
        if (inGetTipProcessing())
            lnTotTransAmt = pobTran->srBRec.lnTipAmount - pobTran->srBRec.lnOldTipAmount;  
        break;
    case VOID:
    case VOID_REFUND:
        if (fChkCPACUpdate == VS_TRUE) {
            lnTotTransAmt -= pobTran->srBRec.lnBaseTransactionAmount;
            if (inGetTipProcessing())
                lnTotTransAmt -= pobTran->srBRec.lnTipAmount;
        }
        break;
    default:
        break;
    }  
    if (fChkCPACUpdate == VS_FALSE) {
        if ((lnTotTransAmt + atol(szPresentAmt)) > atol(szTotAmtAllwd)) {
            vdDisplayMessageFromFile(CPAC_TRANS_AMT_CROSSED_MSG, WARNING_MSG);
            return(VS_ERR);
        }
    } else {
        sprintf(szAmt, "%06ld", lnTotTransAmt + atol(szPresentAmt));
        if ((lnTotTransAmt + atol(szPresentAmt)) > atol(szTotAmtAllwd)) {
            vdDisplayMessageFromFile(CPAC_TRANS_AMT_CROSSED_MSG, WARNING_MSG);
            return(VS_ERR);
        }
        vdSetCPACPresentAmt(szAmt);
        if (inSaveCPACRec(&srCPAC) != VS_SUCCESS)
            return(VS_ERR);
    }
    return(VS_SUCCESS);
}

/*-----------------------------------------------------------------------------
* Routine Name : int inTotalTransAmtAlwd(TRANSACTION_OBJECT *pobTran)
* Description  : This function accepts the amount for the transaction 
*                if transaction amount matches the different criteria of
*                total transaction amount allowed and current transaction 
*                     total in the terminal
* Inputs       : None
* Outputs      : None
* Returns      : VS_SUCCESS/VS_ERR
* Notes        : 
*---------------------------------------------------------------------------
*/
int inTotalTransAmtAlwd(TRANSACTION_OBJECT* pobTran) {
    if (inChkTransAlwd(pobTran, VS_FALSE) == VS_ERR)
        return(VS_ERR);
    return(VS_SUCCESS);
}


/*-----------------------------------------------------------------------------
* Routine Name : int inCPACTransAmtUpdate(TRANSACTION_OBJECT *pobTran)
* Description  : Updates the CPAC based on the Trans type with the Transaction
*                Amt entered 
* Inputs       : None
* Outputs      : None
* Returns      : VS_SUCCESS/VS_ERR
* Notes        : 
*---------------------------------------------------------------------------
*/
int inCPACTransAmtUpdate(TRANSACTION_OBJECT* pobTran) {
    if (inChkTransAlwd(pobTran, VS_TRUE) == VS_ERR)
        return(VS_ERR);
    return(VS_SUCCESS);
}

VS_BOOL fGetCPACRRNPrint(void) {
    return(srCPAC.fRRNPrint);
}

void vdSetCPACRRNPrint(int inRRNPrint) {
    srCPAC.fRRNPrint = (unsigned) (int) inRRNPrint;
}

VS_BOOL fGetCPACUsrInvNum(void) {
    return(srCPAC.fUsrInvNum);
}

void vdSetCPACUsrInvNum(int inUsrInvNum) {
    srCPAC.fUsrInvNum = (unsigned) (int) inUsrInvNum;
}


void vdSetCPACMaxAmtEntryDigits(short  inMaxAmtEntryDigits) {
    vdLoadCPACParams(&srCPAC);  
    srCPAC.inMaxAmtEntryDigits = inMaxAmtEntryDigits;
}

int inGetCPACMaxAmtEntryDigits(void) {
    vdLoadCPACParams(&srCPAC);  
    return (srCPAC.inMaxAmtEntryDigits);
}

VS_BOOL fGetCPACMerchEnable(void) // saritha
{
    return(srCPAC.fMerchEnable);
}

void vdSetCPACMerchEnable(int inMerchEnable) // saritha
{
    srCPAC.fMerchEnable = (unsigned) (int) inMerchEnable;
}

VS_BOOL fGetCPACMaskPANinMRcpt(void) // Kelvin_F2
{
    return(srCPAC.fMaskPANinMRcpt);
}

void vdSetCPACMaskPANinMRcpt(int inMaskPANinMRcpt) // Kelvin_F2
{
    srCPAC.fMaskPANinMRcpt = (unsigned) (int) inMaskPANinMRcpt;
}

VS_BOOL fGetCPACMaskPANinCRcpt(void) // Kelvin_F2
{
    return(srCPAC.fMaskPANinCRcpt);
}

void vdSetCPACMaskPANinCRcpt(int inMaskPANinCRcpt) // Kelvin_F2
{
    srCPAC.fMaskPANinCRcpt = (unsigned) (int) inMaskPANinCRcpt;
}

VS_BOOL fGetCPACMaskPANinBRcpt(void) // Kelvin_F2
{
    return(srCPAC.fMaskPANinBRcpt);
}

void vdSetCPACMaskPANinBRcpt(int inMaskPANinBRcpt) // Kelvin_F2
{
    srCPAC.fMaskPANinBRcpt = (unsigned) (int) inMaskPANinBRcpt;
}

void vdGetCPACPassword1(char* pchPass1) {
    strcpy(pchPass1, srCPAC.szPassword1);
}

void vdSetCPACPassword2(char* pchPass2) {
    strcpy(srCPAC.szPassword2, pchPass2);
}


void vdGetCPACPassword3(char* pchPass3) {
    strcpy(pchPass3, srCPAC.szPassword3);
}

void vdSetCPACPassword3(char* pchPass3) {
    strcpy(srCPAC.szPassword3, pchPass3);
}

void vdGetCPACPassword4(char* pchPass4) {
    strcpy(pchPass4, srCPAC.szPassword4);
}

void vdSetCPACPassword4(char* pchPass4) {
    strcpy(srCPAC.szPassword4, pchPass4);
}

void vdGetCPACPassword5(char* pchPass5) {
    strcpy(pchPass5, srCPAC.szPassword5);
}

void vdSetCPACPassword5(char* pchPass5) {
    strcpy(srCPAC.szPassword5, pchPass5);
}


VS_BOOL fGetCPACStanInc(void) // Saritha_b1
{
    return(srCPAC.fStanInc);
}

void vdSetCPACStanInc(int inStanInc) // Saritha_b1
{
    srCPAC.fStanInc = (unsigned) (int) inStanInc;
}

/*sowmya*/
char* szGetCPACTID(void) {
    return srCPAC.szCPACTID;
}

void vdSetCPACTID(const char* szCPACTID) {
    strcpy(srCPAC.szCPACTID, szCPACTID);
}

char* szGetCPACMID(void) {
    return srCPAC.szCPACMID;
}

void vdSetCPACMID(const char* szCPACMID) {
    strcpy(srCPAC.szCPACMID, szCPACMID);
}

char* szGetCPACNII(void) {
    return srCPAC.szCPACNII;
}

void vdSetCPACNII(char* szCPACNII) {
    strcpy(srCPAC.szCPACNII, szCPACNII);
}
char* szGetCPACTPDU(void) {
    return srCPAC.szCPACTPDU;
}

void vdSetCPACTPDU(char* szCPACTPDU) {
    strcpy(srCPAC.szCPACTPDU, szCPACTPDU);
}

